﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControllerClient
{
    class VerifyLicense
    {
        private string beginTime = string.Empty;
        public string BeginTime
        {
            get { return beginTime; }
            set { beginTime = value; }
        }

       

        public void verify()
        {
            int dayDiff = -1;
            
            try
            {

                string info = string.Empty;
                string msg = string.Empty;
                //初始化加载
                if (LicenceHelper.CheckLicenceKeyIsExists())
                {
                    string keyFile = LicenceHelper.GetDefaultRegisterFileName();
                    info = LicenceHelper.ReadLicenceKey(keyFile);
                }
                else
                {
                    var dialogResult = MessageBox.Show("没有找到默认安装许可文件，是否手动选择安装许可文件？", "询问", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        OpenFileDialog openFileDialog = new OpenFileDialog();
                        openFileDialog.Title = "请选择安装许可文件";
                        openFileDialog.FileName = LicenceHelper.GetDefaultRegisterFileName();
                        if (openFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            var keyFile = openFileDialog.FileName;
                            info = LicenceHelper.ReadLicenceKey(keyFile);
                            //验证成功后，将手动选择的文件复制到程序根目录,且修改为默认名称
                            File.Copy(keyFile, LicenceHelper.GetDefaultRegisterFileName());

                        }
                        else
                        {
                            string computerFile = LicenceHelper.GetComputerInfoAndGenerateFile();
                            if (!string.IsNullOrEmpty(computerFile))
                            {
                                msg = $"您还没有安装许可文件，请将程序根目录下的{computerFile}文件，发送到管理员，获取Licence.";
                            }
                        }
                    }
                    else
                    {
                        string computerFile = LicenceHelper.GetComputerInfoAndGenerateFile();
                        if (!string.IsNullOrEmpty(computerFile))
                        {
                            msg = $"您还没有安装许可文件，请将程序根目录下的{computerFile}文件，发送到管理员，获取Licence.";
                        }
                    }
                }
                if (!string.IsNullOrEmpty(info) && string.IsNullOrEmpty(msg))
                {
                    string[] infos = info.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    if (infos.Length > 0)
                    {
                        var dicInfo = new Dictionary<string, string>();
                        foreach (var info2 in infos)
                        {
                            if (string.IsNullOrEmpty(info2))
                            {
                                continue;
                            }
                            var info2Arr = info2.Split('=');
                            dicInfo.Add(info2Arr[0], info2Arr[1]);
                            Console.WriteLine("=======" + info2Arr[0] + "===" + info2Arr[1]);
                        }
                        if (dicInfo.Count > 0)
                        {
                            string localMacAddress = string.Empty;
                            var computerInfo = LicenceHelper.GetComputerInfo();
                            if (computerInfo != null)
                            {
                                localMacAddress = computerInfo["mac"];
                            }
                            //比较本地信息和Licence中的信息是否一致
                            if (localMacAddress.Contains( dicInfo["mac"]))
                            {
                                var endTime = DateTime.Parse(dicInfo["endTime"]);
                                var beginTime = DateTime.Parse(dicInfo["beginTime"]);
                                Console.WriteLine("DateTime.Now" + DateTime.Now);
                                if (DateTime.Now < endTime)
                                {
                                    //在有效期内，可以使用
                                }
                                else
                                {
                                    msg = $"软件授权使用时间范围：[{endTime}之前]，已过期";
                                }
                                //VerifyLicense vl = new VerifyLicense();
                                this.beginTime = dicInfo["beginTime"];
                                //Console.WriteLine("beginTime" + beginTime);
                                //TXTHelper ltInstallDiff = new TXTHelper("installDiff.ini", 1);
                                //ltInstallDiff.WriteMessage(dicInfo["beginTime"]);
                                //DateTime beginDateTime = Convert.ToDateTime(beginTime);
                                ////计算安装日期和当前日期的时间差
                                //TimeSpan timeSpan = DateTime.Now.Subtract(beginDateTime);
                                //dayDiff = timeSpan.Days ;
                                //TXTHelper ltDayDiff = new TXTHelper("dayDiff.ini", 1);
                                
                                //DateTime detalineTime = Convert.ToDateTime("2024-06-15");
                                //if (beginDateTime < detalineTime)
                                //{
                                //    dayDiff = 1;
                                //}
                                //else
                                //{

                                //}
                                //ltDayDiff.WriteMessage(dayDiff.ToString());

                            }
                            else
                            {
                                msg = "软件Licence不匹配";
                            }
                        }
                        else
                        {
                            msg = $"软件Licence非法.";
                        }
                    }
                    else
                    {
                        msg = $"软件Licence非法.";
                    }
                }
                if (!string.IsNullOrEmpty(msg))
                {
                    Console.WriteLine("msg" + msg);
                    MessageBox.Show(msg);
                    
                    if (msg.Contains("没有安装许可文件")|| msg.Contains("已过期"))
                    {
                        Application.Exit();
                    }
                   
                    //return;
                }
            }
            catch (Exception ex)
            {
                string error = $"程序异常，请联系管理人员：{ex.Message}\r\n{ex.StackTrace}";
                MessageBox.Show(error);
                Application.Exit();

            }
        }
    }
}
